<template>
    <div class="weather">
      <div class="current-time">{{ currentTime }}</div>
      <div class="current-weather">
        <div>天气: {{ weatherInfo.skycon }}</div>
        <div>温度: {{ weatherInfo.temperature }}°C</div>
      </div>
    </div>
  </template>
  
  <script>
  import axios from 'axios';
  
  export default {
    data() {
      return {
        api:
          'https://api.caiyunapp.com/v2.6/xyq1bcmorCHIt9wW/117.201509,39.085318/daily?dailysteps=7',
        currentTime: '',
        weatherInfo: {
          skycon: '',
          temperature: ''
        }
      };
    },
    methods: {
      async getWeatherInfo() {
        try {
          const response = await axios.get(this.api);
          if (response.data.status === 'ok') {
            this.weatherInfo.skycon = response.data.result.daily.skycon[0].value;
            this.weatherInfo.temperature = response.data.result.daily.temperature[0].avg.toFixed(1);
          }
        } catch (error) {
          console.error('Error fetching weather data:', error);
        }
      },
      updateTime() {
        const now = new Date();
        this.currentTime = `${now.getFullYear()}年${now.getMonth() + 1}月${now.getDate()}日 ${now.getHours()}:${now.getMinutes().toString().padStart(2, '0')}`;
      }
    },
    created() {
      this.getWeatherInfo();
      this.updateTime();
      setInterval(this.updateTime, 60000); // 更新时间，每60秒更新一次
    }
  };
  </script>
  
  <style scoped>
  .weather {
    display: flex;
    flex-direction: column;
  }
  
  .current-time {
    font-size: 24px;
  }
  
  .current-weather {
    display: flex;
    justify-content: space-between;
    font-size: 18px;
  }
  </style>
  